package com.adopapa.nezhacloud.authc.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import com.adopapa.nezhacloud.redis.RedisTemplateManager;


@Configuration
@EnableCaching
//@RefreshScope
public class RedisConfig  {

	@Value("${redis.host}")
	private String host;
	@Value("${redis.port}")
	private int port;
	@Value("${redis.password}")
	private String password;

//	@Bean
//	public KeyGenerator keyGenerator() {
//		return new KeyGenerator() {
//			@Override
//			public Object generate(Object target, Method method, Object... params) {
//				StringBuilder sb = new StringBuilder();
//				sb.append(target.getClass().getName());
//				sb.append(method.getName());
//				for (Object obj : params) {
//					sb.append(obj.toString());
//				}
//				return sb.toString();
//			}
//		};
//	}

//	@Order(10000)
//	@Bean(name = "connectionFactory", destroyMethod = "destroy")
//	public JedisConnectionFactory connetcionFactory() {
//		RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
//		configuration.setHostName(host);
//		configuration.setPort(port);
//		JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration);
//
//		return connectionFactory;
//	}

//    @SuppressWarnings("rawtypes")
//    @Bean
//    public CacheManager cacheManager(RedisTemplate redisTemplate) {
//        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//        //设置缓存过期时间
//        //rcm.setDefaultExpiration(60);//秒
//        return rcm;
//    }

//	@Bean
//	public CacheManager cacheManager(RedisConnectionFactory factory) {
//		RedisCacheManagerBuilder builder = //
//				RedisCacheManagerBuilder.fromConnectionFactory(factory);//
//		return builder.build();
//	}

//    @Bean("mcRedisTemplate")
//    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
//        StringRedisTemplate template = new StringRedisTemplate(factory);
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        template.afterPropertiesSet();
//        return template;
//    }

//	@Bean("mcRedisTemplate")
//	public RedisTemplateProxy<String, Object> redisTemplateProxy(RedisConnectionFactory connectionFactory) {
//		RedisTemplateProxy<String, Object> redisTemplate = new RedisTemplateProxy<String, Object>();
//		redisTemplate.getsetConnectionFactory(connectionFactory);
//		StringRedisSerializer serializer = new StringRedisSerializer(Charset.forName("UTF-8"));
//		redisTemplate.setKeySerializer(serializer);
//		redisTemplate.setHashKeySerializer(serializer);
//		redisTemplate.afterPropertiesSet();
//
//		return redisTemplate;
//
//	}

	@Order(Ordered.HIGHEST_PRECEDENCE)
	@Bean
	public RedisTemplateManager<String, Object> redisTemplateManager() {
		RedisTemplateManager<String, Object> redisTemplateManager = //
				new RedisTemplateManager<String, Object>(host, port, password);
		return redisTemplateManager;
	}

}